provider "alicloud" {
  region = var.region
}

//根据vpc名字查询vpc
data "alicloud_vpcs" "vpcs" {
  name_regex = var.vpc_name
}
# output "vpcs" {
#   value = data.alicloud_vpcs.vpcs.vpcs
# }

//根据switch名字查询vswitch
data "alicloud_vswitches" "vsws" {
  zone_id = var.zone
  name_regex = var.vsw_name
}
# output "vsws" {
#   value = data.alicloud_vswitches.vsws.vswitches
# }

//根据vpc id查询安全组
data "alicloud_security_groups" "sgs" {
  vpc_id = data.alicloud_vpcs.vpcs.vpcs.0.id
  name_regex = var.sg_name
  # name_regex = "workenv"
}
# output "sgs" {
#   value = data.alicloud_security_groups.sgs.groups
# }

# resource "alicloud_ecs_key_pair" "default" {
#   key_pair_name     = var.key_name
#   public_key        = var.public_key
# }

data "alicloud_ecs_key_pairs" "ekps" {
  name_regex           = var.key_name
  # resource_group_id    = data.alicloud_resource_manager_resource_groups.rmrg.groups.0.id
}

# output "key_pairs" {
#   value = data.alicloud_ecs_key_pairs.ekps.key_pairs
# }

resource "alicloud_instance" "myecs" {
  # count = var.instance_number

  availability_zone                   = var.zone
  security_groups                     = data.alicloud_security_groups.sgs.groups.*.id
  instance_type                       = var.instance_type
  system_disk_category                = var.disk_category
  system_disk_size                    = var.disk_size
  system_disk_auto_snapshot_policy_id = var.snapshot_switch  > 0 ? var.snapshot_policy_id : null
  image_id                            = var.image_id
  instance_name                       = var.instance_name
  internet_max_bandwidth_out          = 0

  # key_name   = var.instance_password == "" ? alicloud_ecs_key_pair.default.key_pair_name : ""
  key_name             = var.instance_password == "" ? data.alicloud_ecs_key_pairs.ekps.key_pairs.0.key_pair_name : ""
  vswitch_id           = data.alicloud_vswitches.vsws.vswitches.0.id

  # 设置实例的抢占式属性
  spot_strategy        = var.instance_spot_strategy
  spot_price_limit     = var.instance_spot_price_limit

  # windows密码
  password             = var.instance_password

  #指定私网IP
  private_ip           = var.instance_private_ip

  # 根据配置创建数据盘
  dynamic data_disks {
    for_each = var.data_disk_count  > 0 ? [1] : []
    content {
      name                    = var.data_disk_name
      size                    = var.data_disk_size
      category                = var.data_disk_category
      auto_snapshot_policy_id = var.snapshot_switch  > 0 ? var.snapshot_policy_id : null
      device                  = var.data_disk_device != null ? var.data_disk_device : null
    }
  }

  # 使用 user_data 参数传递 Cloud-Init 脚本
  user_data = <<-EOF
              #!/bin/bash
              # 自动挂载数据盘的脚本
              for device in /dev/vd[b-z]; do
                if lsblk | grep -q $(basename $device); then
                  mount_point="/mnt/dd$(basename $device)"
                  mkdir -p $mount_point
                  mkfs.ext4 $device
                  mount $device $mount_point
                  echo "$device $mount_point ext4 defaults 0 0" >> /etc/fstab
                fi
              done
              EOF

  tags = {
    application = var.tags_application
    owner = var.tags_owner
  }
  volume_tags = {
    application = var.tags_application
    owner = var.tags_owner
  }
}

resource "ansible_host" "gram_instance" {
  count                     = var.ansible_switch > 0 ? 1 : 0
  inventory_hostname        = alicloud_instance.myecs.private_ip
  groups                    = ["gram_instance"]
  vars                      = {
    wait_connection_timeout = 600
    private_ip              = alicloud_instance.myecs.private_ip
    application_name        = var.ansible_application_name
    db_host                 = var.ansible_db_host
    db_port                 = var.ansible_db_port
    db_database             = var.ansible_db_database
    db_nexus3_database      = var.ansible_db_nexus3_database
    db_gogs_database        = var.ansible_db_gogs_database
    db_username             = var.ansible_db_username
    db_password             = var.ansible_db_password
    db_database             = var.ansible_db_database
    redis_host              = var.ansible_redis_host
    redis_port              = var.ansible_redis_port
    redis_password          = var.ansible_redis_password
    redis_username          = var.ansible_redis_username
    gogs_host               = var.ansible_gogs_host
    gogs_token              = var.ansible_gogs_token
    oss_dir                 = var.ansible_oss_dir
    commit                  = var.ansible_commit
    root_key_name           = var.ansible_root_key_name
    read_key_name           = var.ansible_read_key_name
  }
}

resource "alicloud_eip_address" "eip" {
  count = var.eip_count > 0 ? 1 : 0
  tags = {
    application = var.tags_application
    owner = var.tags_owner
  }
}

resource "alicloud_eip_association" "eip_asso" {
  count = var.eip_count > 0 ? 1 : 0
  allocation_id = alicloud_eip_address.eip[count.index].id
  instance_id   = alicloud_instance.myecs.id
}

# 将ECS实例绑定到已存在的虚拟服务器组
resource "alicloud_slb_server_group_server_attachment" "server_attachment" {
  count                     = var.slb_switch > 0 ? 1 : 0
  # count           = var.slb_server_group_server_attachment_count
  server_group_id = var.slb_group_id
  server_id       = alicloud_instance.myecs.id
  port            = var.slb_server_attachment_port
  weight          = var.slb_server_attachment_weight
}

output "prv_ip" {
  value = alicloud_instance.myecs.private_ip
}
